Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(5P) SPT add JS Functional tests to the plugin #35379

Merged
merged 27 commits into from
Sep 20, 2019
Merged

Conversation

getdave
Copy link
Contributor

@getdave getdave commented Aug 14, 2019

This PR aims to add a JS unit test framework to the SPT Plugin via @wordpress/scripts.

However, as Gutenberg Core is considering dropping Enzyme the tests are written in React Testing Library. There is also a custom Jest config to allow us to overide the default settings for our Plugin as required.

All components are now covered by a test. Note that some components do not have their own individual test because they are covered by the parent component as we no longer use shallow rendering.

Please note

  • Some devDependencies have been introduced. These are entirely necessary and have been agreed with @sirreal (see comments below) as being valid.
  • The number of lines of code change figure is high. This is however, due to package-lock.json and @wordpress/scripts dependency. Don't panic.

Testing instructions

  • npm install within the apps/full-site-editing dir
  • Stay within the apps/full-site-editing dir
  • Run npm run test:unit:watch
  • See some example tests pass.
  • Write some more tests to see if they get picked up.

@getdave getdave requested a review from a team August 14, 2019 16:11
@getdave getdave requested review from a team as code owners August 14, 2019 16:11
@getdave getdave self-assigned this Aug 14, 2019
@matticbot
Copy link
Contributor

@obenland
Copy link
Member

@getdave You're adding 12k lines of code? :)

@obenland obenland added this to the Ajax: Iteration 7 milestone Aug 14, 2019
@getdave
Copy link
Contributor Author

getdave commented Aug 14, 2019

Wowzers. Why is package-lock.json so large? That's the culprit.

@matticbot
Copy link
Contributor

matticbot commented Aug 14, 2019

This PR does not affect the size of JS and CSS bundles shipped to the user's browser.

Generated by performance advisor bot at iscalypsofastyet.com.

@gwwar
Copy link
Contributor

gwwar commented Aug 14, 2019

Did we commit the package.lock? The dependencies in package.json can likely be removed in a janiortorial all of our dependencies are externalized

@getdave getdave changed the title SPT add JS unit tests to the plugin SPT add JS Functional tests to the plugin Aug 20, 2019
@obenland obenland removed this from the Ajax: Iteration 7 milestone Aug 20, 2019
@getdave getdave force-pushed the try/spt-add-unit-tests branch from a41405b to 7bea67a Compare August 21, 2019 15:46
@getdave
Copy link
Contributor Author

getdave commented Aug 21, 2019

@obenland @gwwar Looks like a good 'ol rebase sorted it Scratch that it didn't. package-lock.json is massive. I have no idea why although "@wordpress/scripts" probably has a lot of deps.

@obenland obenland changed the title SPT add JS Functional tests to the plugin (?P) SPT add JS Functional tests to the plugin Aug 21, 2019
@getdave getdave changed the title (?P) SPT add JS Functional tests to the plugin (5P) SPT add JS Functional tests to the plugin Aug 22, 2019
@getdave getdave force-pushed the try/spt-add-unit-tests branch from 7bea67a to 26e2cf3 Compare August 22, 2019 13:55
@getdave
Copy link
Contributor Author

getdave commented Aug 23, 2019

Did we commit the package.lock

To be clear @gwwar @obenland we are supposed to commit the package-lock file. I believe it is large due to @wordpress/scripts dep being added.

@mmtr
Copy link
Member

mmtr commented Aug 24, 2019

Note that we're removing the package-lock.json file in #35729

@getdave getdave force-pushed the try/spt-add-unit-tests branch from afc1745 to 9c3e5c0 Compare August 27, 2019 09:26
getdave added a commit that referenced this pull request Aug 27, 2019
…stead

This is required because of the Calypso monorepo relying on root dependencies. We don’t want to install @wordpress/scripts in Calypso root so npx is the best interim soluton. See #35379 (comment)
@getdave
Copy link
Contributor Author

getdave commented Aug 27, 2019

When I remove @wordpress/scripts from the package.json I get the

Preset @wordpress/jest-preset-default is invalid:

@sirreal I'm going to install @wordpress/scripts in the root package.json as discussed.

@getdave
Copy link
Contributor Author

getdave commented Aug 29, 2019

I've refactored to use React Testing Library because Enzyme is being "deprecated" by Gutenberg Core

See WordPress/gutenberg#17249

getdave added a commit that referenced this pull request Aug 30, 2019
…stead

This is required because of the Calypso monorepo relying on root dependencies. We don’t want to install @wordpress/scripts in Calypso root so npx is the best interim soluton. See #35379 (comment)
@getdave getdave force-pushed the try/spt-add-unit-tests branch from 2221ee1 to 01a5d35 Compare August 30, 2019 11:39
@getdave
Copy link
Contributor Author

getdave commented Aug 30, 2019

@obenland @gwwar I think this is now ready to go. I'd love if you could give it another go and a 👍

@sirreal Can you confirm you're happy with my devDependencies. Had to add a couple more to get Jest config to work. Unavoidable I'm afraid.

@getdave getdave requested review from sirreal and obenland August 30, 2019 11:46
@sirreal
Copy link
Member

sirreal commented Aug 30, 2019

@sirreal Can you confirm you're happy with my devDependencies. Had to add a couple more to get Jest config to work. Unavoidable I'm afraid.

Given this is operating as a fairly isolated project, and I don't think there's a good way around that now, no problem from me 👍

…stead

This is required because of the Calypso monorepo relying on root dependencies. We don’t want to install @wordpress/scripts in Calypso root so npx is the best interim soluton. See #35379 (comment)
…Gutenberg Core

Rewrote tests using RTL and it is much nicer and avoids testing implementation details. I have much more confident in my tests.
@wordpress/jest-preset-default is required in order to extend the @wordpress/scripts jest config
@getdave getdave force-pushed the try/spt-add-unit-tests branch from 01a5d35 to 6589029 Compare September 20, 2019 09:18
Exists as a pass through component to simplying testing components which consume `BlockPreview` from
`@wordpress/block-editor`. This is because jest cannot mock node modules that are not part of the root node modules. Due to the way this projects dependencies are defined
`@wordpress/block-editor` does not exist within `node_modules` and it is there impossible to mock it without providing a wrapping
component to act as a pass though. See https://jestjs.io/docs/en/manual-mocks
Previously adding conditionals in tests had removed the ability to provide a “blank” value in the Templates. Now add this as an explicit assertion across multiple tests to ensure it isn’t lost again.
Copy link
Contributor

@frontdevde frontdevde left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the inline comments @getdave, that made it easier to follow what your thought process behind certain decisions was e.g. the pass-through component. Testing it locally, the tests are passing as expected. With the comments and existing discussions in mind, nothing immediately jumps out to me right now. Given that it shouldn't affect Calypso but just our plugin, I'd be OK with getting this in and continue iterating from there 👍

Copy link
Contributor

@retrofox retrofox left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The changes purposed here don’t affect the current functionality of the plugin, and the tester framework looks pretty good.
I suggest 🚢 this tool since it will helpful for our development flow, considering that we can rollback if we find out another testing approach.
Thanks for taking over of this task, Dave.

@getdave
Copy link
Contributor Author

getdave commented Sep 20, 2019

Thanks @retrofox and @frontdevde. Appreciate your feedback and 👍 on this PR. Let's hope it helps us 🚢 with greater confidence.

Whilst it is important to test for the presence of `blocks` it does not hurt to provide a default value as this ensures the “blank” state can be provided when in dynamic mode.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants